#include <iostream>
#include <algorithm>
#include <string>
#include <vector>

using namespace std;

template<typename Type>
void permu(Type &a, int k, int m)
{
    int i, j;
    if (k == m) {
        for (i = 0; i <= m; i++)
            cout << a[i] << ' ';
        cout << endl;
    }
    else {
        for (j = k; j <= m; j++) {
            swap(a[j], a[k]);
            permu(a, k + 1, m);
            swap(a[j], a[k]);
        }
    }
}

template<typename Type>
void permu_if(Type &a, int len) 
{
    int i, flag;
    sort(a, a + len);

    do {
        for (i = 0; i < len; i++) {
            if (a[i] == 3)
                flag = 1;
            else if (a[i] == 4)
                flag = 2; 
        }

        if (flag == 1) {
            for (i = 0; i < len; i++)
                cout << a[i] << ' ';
            cout << endl;
        }
    }while(next_permutation(a, a + len)); 
} 

void per()
{
    vector<int> data;
    int n, ix;
    cout << "Enter the num.: " ;
    cin >>  n;

    for (ix = 0; ix < n; ix ++)
        data.push_back(rand()%100);

    cout << endl;
    for (ix = 0; ix < data.size(); ix++) {
         cout << data[ix] << ' ';            
    }
    cout << endl;
    //sort(data.begin(), data.end());
    cout << endl << "next_permutation:\n"; 
    while (next_permutation(data.begin(), data.end())) {
        for (ix = 0; ix < data.size(); ix++) 
             cout << data[ix] << ' ';
             cout << endl;
    } 
}

int main()
{
    int a[5] = {1, 2, 3, 4, 5};
    
    cout << "all the permutation: " << endl;
    //permu(a, 0, 4);
    permu_if(a, 5);

    return 0;
}
